package com.example.jinggai;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class KaiqiActivity extends AppCompatActivity {

    Button ok,no;
    EditText time,people,et_jinggai;
    private static MqttClient mqttClient;
    private static MqttConnectOptions mqttConnectOptions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kaiqi);


        ok = findViewById(R.id.btn_ok);
        no = findViewById(R.id.btn_no);


        time = findViewById(R.id.et_time);
        people = findViewById(R.id.et_people);
        et_jinggai = findViewById(R.id.et_jinggai);


        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time.setText("");
                et_jinggai.setText("");

                String topic = "conment";
                String content = "申请同意了";
                int qos = 0;
                MqttMessage message = new MqttMessage(content.getBytes());
                message.setQos(qos);
                try {
                    mqttClient.publish(topic, message);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time.setText("");
                et_jinggai.setText("");


                String topic = "conment";
                String content = "申请被驳回";
                int qos = 0;
                MqttMessage message = new MqttMessage(content.getBytes());
                message.setQos(qos);
                try {
                    mqttClient.publish(topic, message);
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        });

        initMqttClient();

    }


    private void initMqttClient() {
        mqttConnectOptions  = new MqttConnectOptions();//MQTT的连接设置
        mqttConnectOptions.setCleanSession(true);//清除连接信息
        mqttConnectOptions.setConnectionTimeout(3);// 设置连接超时时间 单位为秒
        mqttConnectOptions.setKeepAliveInterval(60);//心跳包时间60S
        try { mqttClient = new MqttClient(
                "tcp://114.96.74.180:1883",//连接的地址信息
                "android"+System.currentTimeMillis(),//ClientID,使用当前时间戳
                new MemoryPersistence());//
            mqttClient.setCallback(new MqttCallback() {//回调函数
                @Override//连接断开
                public void connectionLost(Throwable throwable) { }
                @Override//接收到消息
                public void messageArrived(String s, MqttMessage mqttMessage)  {
                    Log.e("MqttMsg:", "Topic:"+s+" "+"Msg:"+mqttMessage.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            String source = mqttMessage.toString();

                            try {

                               if(source.contains("shenqing1")){

                                   Date currentDate = new Date(System.currentTimeMillis());
                                   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                   String currentTime = dateFormat.format(currentDate);
                                   time.setText(currentTime);

                                   et_jinggai.setText("井盖1");



                               }
                                if(source.contains("shenqing2")){

                                    Date currentDate = new Date(System.currentTimeMillis());
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    String currentTime = dateFormat.format(currentDate);
                                    time.setText(currentTime);

                                    et_jinggai.setText("井盖2");

                                }
                                if(source.contains("shenqing3")){

                                    Date currentDate = new Date(System.currentTimeMillis());
                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    String currentTime = dateFormat.format(currentDate);
                                    time.setText(currentTime);

                                    et_jinggai.setText("井盖3");

                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }


                        }
                    });
                }
                @Override//没用过
                public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { }
            });
            new Thread(new Runnable() {//用任务
                @Override
                public void run() {

                    try {
                        Thread.sleep(1000);
                        mqttClient.connect(mqttConnectOptions);
                        if (mqttClient.isConnected()) {
                            Log.e("Mqtt Info", "ConnectSuccess");
                            mqttClient.subscribe("shenqing",0);//订阅1111,消息等级0
                        }
                    }catch (Exception e){
                        Log.e("Mqtt Err", e.toString());
                    }
                }
            }).start();
        }
        catch (Exception e){
            Log.e("Mqtt Err", e.toString());
        }


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if(mqttClient.isConnected()){
                mqttClient.close();
            }


        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}